package com.example.testrxjava.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.net.Uri;
import android.text.TextUtils;
import android.widget.Toast;

import com.example.testrxjava.R;
import com.example.testrxjava.base.MyApplication;

import java.util.Random;

/**
 * 项目名称：TestRxjava
 *
 * @Author 郭迎辉
 * Create by 15:06 on 2019/4/17.
 * Describe:
 */
public class CommUtil {

    public static void Toast(Context context,String message){
        Toast.makeText(context,message,Toast.LENGTH_SHORT).show();
    }
    public static void startActivity(Activity activity,Class c){
        Intent intent=new Intent(activity,c);
        MyApplication.getInstance().startActivity(intent);
    }

    /**
     * 打开浏览器
     */
    public static void openBrowser(Context context, String address){
        if (TextUtils.isEmpty(address) || address.startsWith("file://")) {
            CommUtil.Toast(context,context.getString(R.string.article_browser_error));
            return;
        }
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setData(Uri.parse(address));
        if(context.getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY) != null){
            context.startActivity(intent);
        }else {
            CommUtil.Toast(context,context.getString(R.string.open_browser_unknown));
        }
    }

    /**
     * 搜索标签颜色
     */
    public static final int[] TAB_COLORS = new int[]{
            Color.parseColor("#90C5F0"),
            Color.parseColor("#91CED5"),
            Color.parseColor("#F88F55"),
            Color.parseColor("#C0AFD0"),
            Color.parseColor("#E78F8F"),
            Color.parseColor("#67CCB7"),
            Color.parseColor("#F6BC7E")
    };
    public static int randomTagColor() {
        int randomNum = new Random().nextInt();
        int position = randomNum %TAB_COLORS.length;
        if (position < 0) {
            position = -position;
        }
        return TAB_COLORS[position];
    }

    public static boolean isLogin(Context context) {

        String name=SpUtils.getLoginName(context);
        String pwd=SpUtils.getLoginPwd(context);
        if(!TextUtils.isEmpty(name)&&!TextUtils.isEmpty(pwd)){
            return true;
        }
        return false;
    }


}
